package com.thesis.downloader;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

import com.thesis.utils.CONSTANTS;
import com.thesis.utils.CoordinateConversion;
import com.thesis.utils.ReverseGeocoding;

public class busFullRoute {

	private final String FOLDERNAME = "bus/";

	// ==============================

	public void getAllRoute() {

		String filename = "", URL_BUSROUTE;
		boolean luotdi = true;
		// for each route, get info

		filename = FOLDERNAME + "full_googlpolyline.txt";
		Writer out;
		try {
			out = new BufferedWriter(new OutputStreamWriter(
					new FileOutputStream(filename), "UTF-8"));

			for (Integer routeID : CONSTANTS.BUS_COLLECTED_ROUTE_INT) {

				if (routeID < 0) {
					luotdi = false;
					routeID = -routeID;
				} else {
					luotdi = true;
				}

				URL_BUSROUTE = "http://map.ebms.vn/Server.aspx?action=GetFullRoute&TuyenId="
						+ routeID;
				URL_BUSROUTE += (luotdi) ? "&luotdi=true" : "&luotdi=false";

				System.out.println(URL_BUSROUTE);

				URLConnection busRouteConnection;

				URL busList = new URL(URL_BUSROUTE);

				busRouteConnection = busList.openConnection();
				BufferedReader in = new BufferedReader(new InputStreamReader(
						busRouteConnection.getInputStream(), "UTF-8"));

				String inputLine;

				// BEGIN WRTING

				out.write("////------------------------------------------------------\n");
				out.write("var flightPathCoordinates" + routeID +" = [\n");
				
				while ((inputLine = in.readLine()) != null) {
					String[] info = inputLine.split(" ");
					for (String string : info) {
						String[] longlat = string.split(",");
						out.write("\t\t\tnew google.maps.LatLng(" + longlat[1] + "," + longlat[0] + "),\n");
					}
				}
				
				out.write("	];\n");
				out.write("var flightPath" + routeID + "= new google.maps.Polyline({\n");
				out.write("path: flightPathCoordinates"+routeID+",\n");
				out.write("strokeWeight: 6,\n");
				out.write("strokeOpacity: 0.4,\n");
				out.write("strokeColor: \"#000000\"\n");
				out.write("});\n");
				out.write("	flightPath"+routeID+".setMap(map);\n");
				out.write("////------------------------------------------------------\n\n\n\n");
				System.out.println("Successfully get fullRoute " + filename);

			}
			
			
			
			// TEST DATA
			for (Integer routeID : CONSTANTS.BUS_COLLECTED_TESTING_ROUTE_INT) {

				if (routeID < 0) {
					luotdi = false;
					routeID = -routeID;
				} else {
					luotdi = true;
				}

				URL_BUSROUTE = "http://map.ebms.vn/Server.aspx?action=GetFullRoute&TuyenId="
						+ routeID;
				URL_BUSROUTE += (luotdi) ? "&luotdi=true" : "&luotdi=false";

				System.out.println(URL_BUSROUTE);

				URLConnection busRouteConnection;

				URL busList = new URL(URL_BUSROUTE);

				busRouteConnection = busList.openConnection();
				BufferedReader in = new BufferedReader(new InputStreamReader(
						busRouteConnection.getInputStream(), "UTF-8"));

				String inputLine;

				// BEGIN WRTING

				out.write("////------------------------------------------------------\n");
				out.write("var flightTestPathCoordinates" + routeID +" = [\n");
				
				while ((inputLine = in.readLine()) != null) {
					String[] info = inputLine.split(" ");
					for (String string : info) {
						String[] longlat = string.split(",");
						out.write("\t\t\tnew google.maps.LatLng(" + longlat[1] + "," + longlat[0] + "),\n");
					}
				}
				
				out.write("	];\n");
				out.write("var flightTestPath" + routeID + "= new google.maps.Polyline({\n");
				out.write("path: flightTestPathCoordinates"+routeID+",\n");
				out.write("strokeWeight: 6,\n");
				out.write("strokeOpacity: 0.4,\n");
				out.write("strokeColor: \"#F7203D\"\n");
				out.write("});\n");
				out.write("	flightTestPath"+routeID+".setMap(map);\n");
				out.write("////------------------------------------------------------\n\n\n\n");
				System.out.println("Successfully get fullRoute " + filename);

			}
			
			
			
			
			
			
			
			
			out.close();

		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

	}

	// =======================
        
        public double getRouteDistance() {

		String filename = "", URL_BUSROUTE;
		boolean luotdi = true;
                double Sum = 0;
                double lng = 0;
                double lat = 0;
                double Distance = 0;
		// for each route, get info

		try {
			for (Integer routeID : CONSTANTS.BUS_COLLECTED_ROUTE_INT) {
                            Distance = 0;

				if (routeID < 0) {
					luotdi = false;
					routeID = -routeID;
				} else {
					luotdi = true;
				}

				URL_BUSROUTE = "http://map.ebms.vn/Server.aspx?action=GetFullRoute&TuyenId="
						+ routeID;
				URL_BUSROUTE += (luotdi) ? "&luotdi=true" : "&luotdi=false";

				System.out.println(URL_BUSROUTE);

				URLConnection busRouteConnection;

				URL busList = new URL(URL_BUSROUTE);

				busRouteConnection = busList.openConnection();
				BufferedReader in = new BufferedReader(new InputStreamReader(
						busRouteConnection.getInputStream(), "UTF-8"));

				String inputLine;
                                boolean first = true;
				
				while ((inputLine = in.readLine()) != null) {
					String[] info = inputLine.split(" ");
					for (String string : info) {
						String[] longlat = string.split(",");
                                                if (first == true)
                                                {
                                                    lng = Double.parseDouble(longlat[0]);
                                                    lat = Double.parseDouble(longlat[1]);
                                                    first = false;
                                                }
                                                else
                                                {
                                                    Distance += CoordinateConversion.LatLngDistance(Double.parseDouble(longlat[1]), Double.parseDouble(longlat[0]), lat, lng);
                                                    lng = Double.parseDouble(longlat[0]);
                                                    lat = Double.parseDouble(longlat[1]);
                                                }
					}
				}
                                System.out.println("BUS ROUTE: " + routeID + " Distance: " + Distance);
                                Sum += Distance;

			}

		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
                return Sum;

	}

}
